initial import boxroom 0.6.2
[boxroom-stian.git] / vendor / plugins / rubyzip-0.9.1 / lib / quiz1 / t / solutions / Moses Hohman / test_cipher.rb
blobf781137594387ba9c3fbf6c6183c134667829a8e
1 #!/usr/bin/env ruby
2 require 'test/unit'
3 require 'cipher'
5 module Solitaire
6         class TestChunker < Test::Unit::TestCase
7                 def test_chunks
8                         chunker = Chunker.new("Code in Ruby, live longer!")
9                         assert_equal(["CODEI","NRUBY","LIVEL","ONGER"], chunker.chunks)
10                 end
12                 def test_pads_with_Xs
13                         chunker = Chunker.new("sty")
14                         assert_equal(["STYXX"], chunker.chunks)
15                 end
17                 def test_number_chunks
18                         chunker = Chunker.new("Code in Ruby, live longer!")
19                         assert_equal([[3,15,4,5,9], [14,18,21,2,25], [12,9,22,5,12], [15,14,7,5,18]], chunker.number_chunks)
20                 end
22                 def test_to_letters
23                         assert_equal(["CODEI","NRUBY","LIVEL","ONGER"],
24                                 Chunker.to_letters([[3,15,4,5,9], [14,18,21,2,25], [12,9,22,5,12], [15,14,7,5,18]]))
25                 end
26         end
28         class TestKeystream < Test::Unit::TestCase
29                 def setup
30                         @keystream = Keystream.new
31                 end
33                 def test_keystream_letters
34                         chunker = Chunker.new("Code in Ruby, live longer!")
35                         assert_equal(["DWJXH","YRFDG","TMSHP","UURXJ"], @keystream.keystream_letters(chunker.chunks))
36                 end
38                 def test_card_to_letter
39                         assert_equal("", Keystream.card_to_letter(Card.joker(?A)), "A joker")
40                         assert_equal("", Keystream.card_to_letter(Card.joker(?B)), "B joker")
41                         assert_equal("A", Keystream.card_to_letter(Card.new(Suit::CLUBS, Card::ACE)), "AC")
42                         assert_equal("Z", Keystream.card_to_letter(Card.new(Suit::DIAMONDS, Card::KING)), "KD")
43                         assert_equal("A", Keystream.card_to_letter(Card.new(Suit::HEARTS, Card::ACE)), "AH")
44                         assert_equal("Z", Keystream.card_to_letter(Card.new(Suit::SPADES, Card::KING)), "KS")
45                 end
46         end
48         class TestCipher < Test::Unit::TestCase
49                 def test_encrypt
50                         cipher = Cipher.new("Code in Ruby, live longer!")
51                         assert_equal("encrypt", cipher.mode)
52                         assert_equal("GLNCQ MJAFF FVOMB JIYCB", cipher.crypt)
53                 end
55                 def test_decrypt
56                         cipher = Cipher.new("GLNCQ MJAFF FVOMB JIYCB")
57                         assert_equal("decrypt", cipher.mode)
58                         assert_equal("CODEI NRUBY LIVEL ONGER", cipher.crypt)
59                 end
61                 def test_crypt_idempotent
62                         cipher = Cipher.new("GLNCQ MJAFF FVOMB JIYCB")
63                         assert_equal("decrypt", cipher.mode)
64                         assert_equal("CODEI NRUBY LIVEL ONGER", cipher.crypt)
65                         assert_equal("CODEI NRUBY LIVEL ONGER", cipher.crypt)
66                 end
67         end
68 end